package com.sniff.atp.controller;

import com.sniff.atp.constant.LoginConstant;
import com.sniff.atp.entity.ToolsInfo;
import com.sniff.atp.enums.status.PurchaseAbnormalType;
import com.sniff.atp.manager.ToolsInfoMananager;
import com.sniff.atp.response.SniffResponse;
import com.sniff.atp.service.purchase.PurchaseAbnormalOperationService;
import com.sniff.atp.service.purchase.PurchaseAbnormalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.controller
 * @Author: zxy
 * @CreateTime: 2023-02-03 11:13
 * @Description: 采购模块
 * @Version: 1.0
 */
@Api(value = "采购模块", tags = "采购模块")
@RestController
@RequestMapping("/purchase/abnormal")
public class PurchaseAbnormalController {
    @Autowired
    private PurchaseAbnormalOperationService purchaseAbnormalOperationService;

    @Autowired
    private ToolsInfoMananager toolsInfoMananager;

    @Autowired
    private PurchaseAbnormalService purchaseAbnormalService;


    @ApiOperation("成品采购异常单处理，不重拍-仅退款")
    @GetMapping("/supplierOnlyRefound")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "采购订单号")})
    public Object purchaseRefound(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(4L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        purchaseAbnormalOperationService.supplierOnlyRefound(LoginConstant.systemLoginDTO, purchaseNo);
        return SniffResponse.pass("执行成功");
    }

    @ApiOperation("采购异常单处理")
    @GetMapping("/purchaseAbnormal")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseAbnormalOrder", value = "采购异常订单号"),
                        @ApiImplicitParam(name = "operatorNode", paramType = "Integer", value = "操作节点：1 仅退款-不重拍，2 仅退款-重拍-更换sku，3 退货退款-不重拍,4 退货退款-重拍,5 供应商退款-无需退货，7供应商拒绝退款")})
    public Object purchaseRefound(@RequestParam String purchaseAbnormalOrder, @RequestParam Integer operatorNode) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(5L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        purchaseAbnormalOperationService.purchaseAbnormal(LoginConstant.systemLoginDTO, purchaseAbnormalOrder, operatorNode);
        return SniffResponse.pass("执行成功");
    }

    @ApiOperation("采购异常单手动对账")
    @GetMapping("/manualReconciliationCallback")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseAbnormalOrder", value = "采购异常订单号"),
            @ApiImplicitParam(name = "bizType", paramType = "Integer", value = "商品类型: 1-档口;2-线上采购;3-OEM大货;4-OEM样品;5-线下采购;8-线下结算")})
    public Object manualReconciliationCallback(@RequestParam String purchaseAbnormalOrder, @RequestParam Integer bizType) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(6L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        purchaseAbnormalOperationService.manualReconciliationCallback(LoginConstant.systemLoginDTO, purchaseAbnormalOrder, bizType);
        return SniffResponse.pass("执行成功");
    }

    @ApiOperation("提交采购异常")
    @GetMapping("/handleAbnomal")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseOrderNo", value = "采购单号"),
            @ApiImplicitParam(name = "abnormalType", value = "异常类型：1 需用户确认，2 全网断货，3 原商品缺货，4 预售商品，5 不满足最小起订量，6 更换同款链接")})
    public Object handleAbnormal(String purchaseOrderNo, Integer abnormalType){

        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(28L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        List<PurchaseAbnormalType> collect = Arrays.stream(PurchaseAbnormalType.values()).filter(purchaseAbnormalType ->    {
           return purchaseAbnormalType.getType().equals(abnormalType);
        }).distinct().collect(Collectors.toList());

        purchaseAbnormalService.userConfirmProduct(LoginConstant.systemLoginDTO, purchaseOrderNo, collect.get(0));

        return SniffResponse.pass("执行成功");
    }
}
